import javafx.beans.binding.Bindings;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.control.*;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.scene.text.Font;
import javafx.stage.Stage;

public class SettingsMenu extends VBox {
    private RadioButton resolution2;
    private RadioButton resolution3;
    private Button backButton;
    private Stage primaryStage;
    private Slider volumeSlider;

    public SettingsMenu(Stage primaryStage) {
        this.primaryStage = primaryStage;

        minWidthProperty().bind(Bindings.createDoubleBinding(() ->
                (double) GameConstants.getScreenWidth()));
        minHeightProperty().bind(Bindings.createDoubleBinding(() ->
                (double) GameConstants.getScreenHeight()));

        prefWidthProperty().bind(minWidthProperty());
        prefHeightProperty().bind(minHeightProperty());
        maxWidthProperty().bind(minWidthProperty());
        maxHeightProperty().bind(minHeightProperty());

        setAlignment(Pos.CENTER);
        setPadding(new Insets(20));

        VBox content = new VBox(30);
        content.setAlignment(Pos.CENTER);
        content.setPadding(new Insets(50));
        content.setStyle("-fx-background-color: rgba(40, 40, 60, 0.8); -fx-background-radius: 20;");

        content.maxWidthProperty().bind(Bindings.createDoubleBinding(
                () -> GameConstants.getScreenWidth() * 0.8));
        content.minWidthProperty().bind(Bindings.createDoubleBinding(
                () -> GameConstants.getScreenWidth() * 0.6));

        Label title = new Label("游戏设置");
        title.setFont(Font.font("微软雅黑",
                Bindings.createDoubleBinding(
                        () -> GameConstants.getScreenHeight() * 0.05).floatValue()));
        title.setTextFill(Color.WHITE);

        ToggleGroup resolutionGroup = new ToggleGroup();

        resolution2 = createStyledRadioButton("960x540", resolutionGroup);
        resolution3 = createStyledRadioButton("1280x720", resolutionGroup);

        int width = GameConstants.getScreenWidth();
        int height = GameConstants.getScreenHeight();

        if (width == 960 && height == 540) {
            resolution2.setSelected(true);
        } else if (width == 1280 && height == 720) {
            resolution3.setSelected(true);
        } else {
            resolution2.setSelected(true);
        }

        // 添加音量控制滑块
        Label volumeLabel = new Label("BGM 音量");
        volumeLabel.setFont(Font.font("微软雅黑", 20));
        volumeLabel.setTextFill(Color.WHITE);

        volumeSlider = new Slider(0, 1, MusicManager.getVolume());
        volumeSlider.setPrefWidth(300);
        volumeSlider.setShowTickLabels(true);
        volumeSlider.setShowTickMarks(true);
        volumeSlider.setMajorTickUnit(0.25);
        volumeSlider.setBlockIncrement(0.1);
        volumeSlider.setStyle("-fx-control-inner-background: #3498db;");

        // 音量变化时实时调整
        volumeSlider.valueProperty().addListener((obs, oldVal, newVal) -> {
            MusicManager.setVolume(newVal.doubleValue());
        });

        backButton = new Button("返回主菜单");
        MainMenu.styleButton(backButton, Color.rgb(52, 152, 219));

        backButton.prefWidthProperty().bind(
                Bindings.createDoubleBinding(() -> GameConstants.getScreenWidth() * 0.3));
        backButton.prefHeightProperty().bind(
                Bindings.createDoubleBinding(() -> GameConstants.getScreenHeight() * 0.1));

        backButton.setFont(Font.font("微软雅黑",
                Bindings.createDoubleBinding(
                        () -> GameConstants.getScreenHeight() * 0.02).floatValue()));

        content.getChildren().addAll(title, resolution2, resolution3, volumeLabel, volumeSlider, backButton);
        getChildren().add(content);
    }

    private RadioButton createStyledRadioButton(String text, ToggleGroup group) {
        RadioButton button = new RadioButton(text);
        button.setToggleGroup(group);
        button.setFont(Font.font("微软雅黑", 24));
        button.setTextFill(Color.WHITE);
        button.setStyle("-fx-background-color: transparent;");
        button.setPadding(new Insets(10));

        button.setOnMouseEntered(e -> {
            button.setTextFill(Color.YELLOW);
            button.setEffect(new javafx.scene.effect.Glow(0.5));
        });

        button.setOnMouseExited(e -> {
            button.setTextFill(Color.WHITE);
            button.setEffect(null);
        });

        return button;
    }

    public RadioButton getResolution2() {
        return resolution2;
    }

    public Button getBackButton() {
        return backButton;
    }

    public RadioButton getResolution3() {
        return resolution3;
    }
}